package com.atguigu.ocp.improve;

/**
 * 按照开闭原则 优化代码
 */
public class Ocp {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.draw(new Rectangle());
        graphicEditor.draw(new Triangle());
        graphicEditor.draw(new Circle());
    }
}

/**
 * 绘图的类,使用方
 */
class GraphicEditor {
    public void draw(Shape shape) {
        shape.draw();
    }
}

/**
 * Shape,所有图形的基类
 */
abstract class Shape {
    int m_type;

    /**
     * 抽象的画图方法,需要图形子类自己去实现
     */
    public abstract void draw();
}

/**
 * 矩形类
 */
class Rectangle extends Shape {

    public Rectangle() {
        super.m_type = 1;
    }

    @Override
    public void draw() {
        System.out.println("绘制矩形");
    }
}

/**
 * 圆形类
 */
class Circle extends Shape {

    public Circle() {
        super.m_type = 2;
    }

    @Override
    public void draw() {
        System.out.println("绘制圆形");
    }
}

class Triangle extends Shape {

    public Triangle() {
        super.m_type = 3;
    }

    @Override
    public void draw() {
        System.out.println("绘制三角形");
    }
}
